package cn.knightzz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.knightzz.constant.RoleType;
import cn.knightzz.entity.SysBuilding;
import cn.knightzz.entity.SysUser;
import cn.knightzz.mapper.SysBuildingMapper;
import cn.knightzz.service.SysBuildingService;
import cn.knightzz.service.SysFileService;
import cn.knightzz.service.SysUserService;
import cn.knightzz.util.ConverterUtils;
import cn.knightzz.vo.BuildingVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author knightzz
 * @since 2021-05-09
 */
@Service
public class SysBuildingServiceImpl extends ServiceImpl<SysBuildingMapper, SysBuilding> implements SysBuildingService {

    @Resource
    SysBuildingMapper buildingMapper;

    @Resource
    SysFileService fileService;

    @Resource
    SysUserService userService;

    /**
     * 添加建筑物信息
     *
     * @param buildingVO
     * @return
     */
    @Override
    public Boolean addBuilding(BuildingVO buildingVO) {

        // 拷贝属性
        SysBuilding building = new SysBuilding();
        BeanUtil.copyProperties(buildingVO, building, true);

        // 将文件列表转换为字符串
        String files = ConverterUtils.ArrayToStr(buildingVO.getFiles(), ",");
        building.setFiles(files);

        try {
            return buildingMapper.insert(building) != 0;
        } catch (Exception e) {
            e.printStackTrace();
            return  false;
        }
    }

    /**
     * 更新建筑物信息
     *
     * @param buildingVO
     * @return
     */
    @Override
    public Boolean updateBuilding(BuildingVO buildingVO) {

        // 拷贝属性
        SysBuilding building = new SysBuilding();
        BeanUtil.copyProperties(buildingVO, building, true);

        try {
            return buildingMapper.updateById(building) != 0;
        } catch (Exception e) {
            e.printStackTrace();
            return  false;
        }
    }

    /**
     * 删除建筑物信息
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteBuilding(Long id) {

        // 获取建筑物信息
        SysBuilding building = buildingMapper.selectById(id);
        // 获取文件列表字符串
        String files = building.getFiles();
        // 字符串转集合
        List<Long> longs = ConverterUtils.StrToList(files, ",");


        try {
            boolean status = buildingMapper.deleteById(id) != 0;
            if(status){
                for (Long file : longs) {
                    fileService.deleteFile(file);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param query
     * @return
     */
    @Override
    public IPage<SysBuilding> selectByPage(Long userId,Long pageNum, Long pageSize, String query) {

        // 获取当前用户信息
        SysUser userById = userService.getUserById(userId);
        QueryWrapper<SysBuilding> queryWrapper = new QueryWrapper<>();
        // 判断当前用户角色
        switch (userById.getType().intValue()){
            // 超级管理员和网格员管理员获取当前辖区的数据
            case RoleType.SUPER_ADMIN:
            case RoleType.ADMIN:
                queryWrapper.eq("area", userById.getAddress());
                break;
            // 网格员和其他用户获取当前辖区并且自己上传的数据
            default:
                queryWrapper.eq("area", userById.getAddress());
                queryWrapper.eq("author_id", userById.getId());
                break;
        }

        queryWrapper.like("address", query);
        Page<SysBuilding> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        Page<SysBuilding> result = buildingMapper.selectPage(page, queryWrapper);
        List<SysBuilding> records = result.getRecords();

        for (SysBuilding record : records) {
            // 获取文件列表
            HashMap<String, List<String>> files = fileService.getFiles(record.getFiles());
            record.setImages(files.get("images"));
        }
        result.setRecords(records);
        return result;
    }

    @Override
    public List<SysBuilding> getBuildingByAreaLikeAddress(String area, String query) {

        QueryWrapper<SysBuilding> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("area",area);
        queryWrapper.like("address",query);

        List<SysBuilding> sysBuildings = buildingMapper.selectList(queryWrapper);
        for (SysBuilding sysBuilding : sysBuildings) {
            HashMap<String, List<String>> files = fileService.getFiles(sysBuilding.getFiles());
            sysBuilding.setImages(files.get("images"));
        }
        return sysBuildings;
    }
}
